/*
https://leetcode.cn/problems/edit-distance/submissions/584671899/?envType=problem-list-v2&envId=dynamic-programming
72. 编辑距离
*/
class Solution {
public:
    int dp[505][505]={0};
    int minDistance(string word1, string word2) {
        int n=word1.size(),m=word2.size();
        for(int i=0;i<=word2.size();++i){
            dp[0][i]=i;
        }for(int i=0;i<=n;++i){
            dp[i][0]=i;
        }
        for(int i=1;i<=n;++i){
            for(int j=1;j<=m;++j){
                if(word1[i-1]==word2[j-1])  dp[i][j]=dp[i-1][j-1];
                else dp[i][j]=min(min(dp[i-1][j-1],dp[i-1][j]),dp[i][j-1])+1;
            }
        }
        return dp[word1.size()][word2.size()];
    }

};